//
//  MCPaySelectedLotteryTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/6/15.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPaySelectedLotteryTableViewCell.h"
#import "MCBetModel.h"
@interface RangeModel : NSObject
@property(nonatomic)NSRange  range;
@end
@implementation RangeModel
@end


@interface MCPaySelectedLotteryTableViewCell ()

/*
 * 背景
 */
@property (nonatomic,strong)UIImageView * imgV_Back;

/**选号码*/
@property (nonatomic,strong)UILabel * lab_BallNumbers;
/**玩法名称*/
@property (nonatomic,strong)UILabel * lab_WFName;
/**返点和如实、虚拟*/
@property (nonatomic,strong)UILabel * lab_Bonus;
/**注数*/
@property (nonatomic,strong)UILabel * lab_StakeNumber;
/**倍数*/
@property (nonatomic,strong)UILabel * lab_Multiple;
/**购买金额*/
@property (nonatomic,strong)UILabel * lab_PayMoney;
/**删除按钮*/
@property (nonatomic,strong)UIButton * btn_delete;



@end

@implementation MCPaySelectedLotteryTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    self.backgroundColor=[UIColor clearColor];
    
    
    _imgV_Back=[[UIImageView alloc]init];
    [self addSubview:_imgV_Back];
    _imgV_Back.frame=CGRectMake(10, 0, G_SCREENWIDTH-20, 103);
    _imgV_Back.image=[UIImage imageNamed:@"goucai_beijing"];
    _imgV_Back.userInteractionEnabled=YES;

    
    /*
     * 选号码
     */
    _lab_BallNumbers =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_BallNumbers.textColor=[UIColor whiteColor];
    _lab_BallNumbers.font=[UIFont systemFontOfSize:15];
    _lab_BallNumbers.text =@"加载中";
    _lab_BallNumbers.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_BallNumbers];
    
    /*
     * 玩法名称
     */
    _lab_WFName =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_WFName.textColor=[UIColor whiteColor];
    _lab_WFName.font=[UIFont systemFontOfSize:15];
    _lab_WFName.text =@"加载中";
    _lab_WFName.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_WFName];
    
    
    
    /*
     * 金额
     */
    _lab_Bonus =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_Bonus.textColor=[UIColor whiteColor];
    _lab_Bonus.font=[UIFont systemFontOfSize:15];
    _lab_Bonus.text =@"加载中";
    _lab_Bonus.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_Bonus];
    
    /*
     * 注数
     */
    _lab_StakeNumber =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_StakeNumber.textColor=[UIColor whiteColor];
    _lab_StakeNumber.font=[UIFont systemFontOfSize:15];
    _lab_StakeNumber.text =@"加载中";
    _lab_StakeNumber.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_StakeNumber];
    
    
    /*
     * 倍数
     */
    _lab_Multiple =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_Multiple.textColor=[UIColor whiteColor];
    _lab_Multiple.font=[UIFont systemFontOfSize:15];
    _lab_Multiple.text =@"加载中";
    _lab_Multiple.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_Multiple];
    
    
    /*
     * 单价
     */
    _lab_PayMoney =[[UILabel alloc]initWithFrame:CGRectZero];
    _lab_PayMoney.textColor=[UIColor whiteColor];
    _lab_PayMoney.font=[UIFont systemFontOfSize:15];
    _lab_PayMoney.text =@"加载中";
    _lab_PayMoney.textAlignment=NSTextAlignmentLeft;
    [_imgV_Back  addSubview:_lab_PayMoney];
    
    /*
     * 删除按钮
     */
    _btn_delete =[[UIButton alloc]initWithFrame:CGRectZero];
    [_btn_delete setImage:[UIImage imageNamed:@"goucai_delete"] forState:UIControlStateNormal];
    [_imgV_Back  addSubview:_btn_delete];
    [_btn_delete addTarget:self action:@selector(action_delete:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self layOutConstraints];
    
    
}

-(void)action_delete:(UIButton* )btn{
    
    _firstResbanderZhuiHaoView = NO;
    if (self.block) {
        self.block();
    }
    
}

-(void)layOutConstraints{
    /*
     * 选号码
     */
    [_lab_BallNumbers mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imgV_Back.mas_left).offset(20);
        make.top.equalTo(_imgV_Back.mas_top).offset(15);
        make.right.equalTo(_imgV_Back.mas_right).offset(-20);
//        make.height.mas_equalTo(20);
    }];
    
    
    
    /*
     * 玩法名称
     */
    [_lab_WFName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_imgV_Back.mas_centerY);
        make.left.equalTo(_imgV_Back.mas_left).offset(20);
        make.height.mas_equalTo(20);
        make.right.equalTo(_lab_BallNumbers.mas_right);
    }];
    
    
    
    /*
     * 金额
     */
    [_lab_Bonus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imgV_Back.mas_left).offset(150);
        make.centerY.equalTo(_lab_WFName.mas_centerY);
        make.height.mas_equalTo(20);
        make.right.equalTo(_lab_BallNumbers.mas_right);
    }];
    
    
    
    /*
     * 注数
     */
    [_lab_StakeNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_imgV_Back.mas_left).offset(20);
        make.bottom.equalTo(_imgV_Back.mas_bottom).offset(-15);
    }];
    
    
    /*
     * 倍数
     */
    [_lab_Multiple mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lab_StakeNumber.mas_right).offset(20);
        make.bottom.equalTo(_imgV_Back.mas_bottom).offset(-15);
        make.centerY.equalTo(_lab_StakeNumber.mas_centerY);
    }];
    
    
    /*
     * 单价
     */
    [_lab_PayMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lab_Multiple.mas_right).offset(20);
        make.bottom.equalTo(_imgV_Back.mas_bottom).offset(-15);
        make.centerY.equalTo(_lab_StakeNumber.mas_centerY);
    }];
    
    
    /*
     * 删除按钮
     */
    [_btn_delete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_imgV_Back.mas_right).offset(-20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(30);
        make.centerY.equalTo(_imgV_Back.mas_centerY);
        
    }];
    
    
}

-(void)setDataSource:(MCPaySelectedCellModel*)dataSource{
    _dataSource=dataSource;
    
    
    _lab_BallNumbers.text=_dataSource.haoMa;
    
    /**玩法名称*/
    _lab_WFName.text=_dataSource.WFName;
    /**返点和如实、虚拟*/
    _lab_Bonus.text=[NSString stringWithFormat:@"%@ %@",_dataSource.showRebate,_dataSource.yuanJiaoFen];
    /**注数*/
    _lab_StakeNumber.text=[NSString stringWithFormat:@"%@注",_dataSource.stakeNumber];
    /**倍数*/
    _lab_Multiple.text=[NSString stringWithFormat:@"%@倍",_dataSource.multiple];
    /**购买金额*/
    _lab_PayMoney.text=[NSString stringWithFormat:@"%@元",GetRealSNum(_dataSource.payMoney)];
    
    
    
//    NSMutableArray *rangeS=[self getRangeStr:_lab_BallNumbers.text findText:@"|"];
//    [self setTextColor:_lab_BallNumbers FontNumber:[UIFont systemFontOfSize:15] AndRangeS:rangeS AndColor:RGB(0, 0, 0)];
    
}


//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRangeS:(NSMutableArray *)rangeS AndColor:(UIColor *)vaColor{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    for (RangeModel * model in rangeS) {
        //设置字号
        [str addAttribute:NSFontAttributeName value:font range:model.range];
        //设置文字颜色
        [str addAttribute:NSForegroundColorAttributeName value:vaColor range:model.range];
    }
    
    label.attributedText = str;
}


+(CGFloat)computeHeight:(id)info{
    
    return 103+10;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

#pragma mark - 获取这个字符串ASting中的所有abc的所在的index
- (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText{
    NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:3];
    if (findText == nil && [findText isEqualToString:@""]){
        return nil;
    }
    NSRange rang = [text rangeOfString:findText]; //获取第一次出现的range
    if (rang.location != NSNotFound && rang.length != 0){
        RangeModel * model =[[RangeModel alloc]init];
        model.range=rang;
        [arrayRanges addObject:model];//将第一次的加入到数组中
        NSRange rang1 = {0,0};
        NSInteger location = 0;
        NSInteger length = 0;
        for (int i = 0;; i++){
            if (0 == i){
                //去掉这个abc字符串
                location = rang.location + rang.length;
                length = text.length - rang.location - rang.length;
                rang1 = NSMakeRange(location, length);
            }else{
                location = rang1.location + rang1.length;
                length = text.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            }
            //在一个range范围内查找另一个字符串的range
            rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
            RangeModel * model1 =[[RangeModel alloc]init];
            model1.range=rang1;
            if (rang1.location == NSNotFound && rang1.length == 0){
                break;
            }else//添加符合条件的location进数组
                [arrayRanges addObject:model1];
        }
        return arrayRanges;
    }
    return nil;
}

@end














































